<?php
	include './init.php';

	$a=$_GET['a'];

	switch ($a) {
		case 'add':
			$id=$_POST['id'];
			$num=$_POST['num'];
			if ($num<1) $num=1;

			//判断是否重复购买
			if (!empty($_SESSION['car'][$id])) 
			{
				$_SESSION['car'][$id]['num']+=$num;
				//var_dump($_SESSION['car']);
				header('location:'.URL.'/car.php');
				return false;
			}

			//查询商品信息
			$model=new Model('goods');
			$sql_goods="select
							g.id        gid,
							g.name      gname,
							g.price     gp,
							g.stock     gs,
							gi.name     giname,
							g.addtime   gtime
						from
							goods       g,
							goods_img   gi
						where
							g.id=gi.goods_id
						and
							gi.is_cover=1
						and
							g.id=$id
						";
			$list=$model->find($sql_goods)[0];
			$list['num']=$num;

			//把商品信息放进session
			$_SESSION['car'][$id]=$list;
			header('location:'.URL.'/car.php');
			break;
		
		case 'del':
			$id=$_GET['id'];
			unset($_SESSION['car'][$id]);
			header('location:'.URL.'/car.php');
			break;

		case 'reset':
			unset($_SESSION['car']);
			header('location:'.URL.'/car.php');
			break;
	}



?>